<?php

class Task extends CI_Model {

    private $taskID;
    private $taskType;
    private $layer;
    private $pid;
    private $status;
    private $startDate;
    private $endDate;
    private $cancelDate;
    private $lastOutputModification;
    private $tilesProcessed;
    private $tilesPerRequest;
    private $intents;

    public function getTaskID() {
        return $this->taskID;
    }

    public function setTaskID($taskID) {
        $this->taskID = $taskID;
    }

    public function getTaskType() {
        return $this->taskType;
    }

    public function setTaskType($taskType) {
        $this->taskType = $taskType;
    }

    public function getPid() {
        return $this->pid;
    }

    public function setPid($pid) {
        $this->pid = $pid;
    }

    public function getStatus() {
        return $this->status;
    }

    public function setStatus($status) {
        $this->status = $status;
    }

    public function getStartDate() {
        return $this->startDate;
    }

    public function setStartDate($startDate) {
        $this->startDate = $startDate;
    }

    public function getEndDate() {
        return $this->endDate;
    }

    public function setEndDate($endDate) {
        $this->endDate = $endDate;
    }

    public function getCancelDate() {
        return $this->cancelDate;
    }

    public function setCancelDate($cancelDate) {
        $this->cancelDate = $cancelDate;
    }

    public function getLayer() {
        return $this->layer;
    }

    public function setLayer($layer) {
        $this->layer = $layer;
    }

    public function getLastoutputModification() {
        return $this->lastOutputModification;
    }

    public function setLastOutputModification($lastOutputModification) {
        $this->lastOutputModification = $lastOutputModification;
    }

    public function getTilesProcessed() {
        return $this->tilesProcessed;
    }

    public function setTilesProcessed($tilesProcessed) {
        $this->tilesProcessed = $tilesProcessed;
    }

    public function getTilesPerRequest() {
        return $this->tilesPerRequest;
    }

    public function setTilesPerRequest($tilesPerRequest) {
        $this->tilesPerRequest = $tilesPerRequest;
    }

    public function getIntents() {
        return $this->intents;
    }

    public function setIntents($intents) {
        $this->intents = $intents;
    }

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    public function createTask($pid, $layer, $taskType, $status, $metatileX, $metatileY) {
        try {
            date_default_timezone_set("America/Argentina/Buenos_Aires");
            $tilesPerRequest = intval($metatileX) * intval($metatileY);
            $data = array(
                'pid' => $pid,
                'layer' => $layer,
                'taskType' => $taskType,
                'startDate' => date('Y-m-d H:i:s'),
                'lastOutputModification' => date('Y-m-d H:i:s'),
                'status' => $status,
                'tilesProcessed' => 0,
                'tilesPerRequest' => $tilesPerRequest > 0 ? $tilesPerRequest : 1
            );
            if ($this->db->insert('task', $data)) {
                $data['taskID'] = $this->db->insert_id();
                return $data;
            }
        } catch (Exception $e) {
            return FALSE;
        }
    }

    public function getTaskByID($taskID) {
        try {
            $this->db->where('taskID', $taskID);
            $query = $this->db->get('task');
            $rows = $query->result();
            return empty($rows) ? FALSE : $rows[0];
        } catch (Exception $e) {
            return FALSE;
        }
    }

    public function getRunningTask() {
        try {
            $this->db->where('status', 1);
            $this->db->where('endDate', null);
            $this->db->where('cancelDate', null);
            $query = $this->db->get('task');
            $rows = $query->result();
            return $rows;
        } catch (Exception $e) {
            return FALSE;
        }
    }

    public function finishTask($taskID, $pid, $status) {
        try {
            date_default_timezone_set("America/Argentina/Buenos_Aires");
            $this->db->where('status', 1);
            $this->db->where('pid', $pid);
            $this->db->where('taskID', $taskID);
            $this->db->set('status', $status); // 2-> terminado con éxito 4->terminado con error
            $this->db->set('endDate', date('Y-m-d H:i:s'));
            return $this->db->update('task');
        } catch (Exception $e) {
            return FALSE;
        }
    }

    public function cancelTask($taskID, $pid) {
        try {
            date_default_timezone_set("America/Argentina/Buenos_Aires");
            $this->db->where('status', 1);
            $this->db->where('pid', $pid);
            $this->db->where('taskID', $taskID);
            $this->db->set('status', 3); // 3-> cancelado por el usuario
            $this->db->set('cancelDate', date('Y-m-d H:i:s'));
            return $this->db->update('task');
        } catch (Exception $e) {
            return FALSE;
        }
    }

    public function isTaskRunningOnLayer($layer) {
        try {

            $this->db->where('status', 1);
            $this->db->where('layer', $layer);
            $this->db->where('cancelDate', null);
            $this->db->where('endDate', null);
            $query = $this->db->get('task');
            $rows = $query->result();
            return $rows;
        } catch (Exception $e) {
            return FALSE;
        }
    }

    public function updateTask($taskID, $countMetatiles, $tilesPerReq) {
        try {
            date_default_timezone_set("America/Argentina/Buenos_Aires");
            $this->db->where('taskID', $taskID);
            $this->db->set('lastOutputModification', date('Y-m-d H:i:s'));
            $this->db->set('tilesProcessed', $countMetatiles * $tilesPerReq);
            return $this->db->update('task');
        } catch (Exception $e) {
            return FALSE;
        }
    }

    public function updateTaskIntents($intents) {
        try {
            $this->db->set('intents', $intents);
            return $this->db->update('task');
        } catch (Exception $e) {
            return FALSE;
        }
    }

}

?>
